import 'cube.css/src/scss/neat.scss'
import 'static/font/iconfont.css'
import 'element-ui/lib/theme-default/index.css'
import './babytime.scss'
import '@/base/scss/common.scss'
import Vue from 'vue'
import ElementUI from 'element-ui'
import UnifyHeader from 'components/header/header'
import UnifyFooter from 'components/footer/footer'
import Suspension from 'components/suspension/suspension'
import mixins from '@/mixins'
import Api from '@/api.js'
import Utils from '../../components/component/util'
import {DEVELOPMENT, PRODUCTION} from "../../config"

let apiHost = process.env.NODE_ENV === 'development' ? DEVELOPMENT : PRODUCTION

Vue.mixin(mixins)
Vue.use(ElementUI)
Vue.use(Utils)

let bbsSlsectDate = {}

// eslint-disable-next-line no-new
new Vue({
  el: '#app',
  data: {
    showSuspensionHeight: 0, // 显示悬浮组件的高度
    picturePrefix: `http://${apiHost.IMAGE_API_HOST}/`,
    nowSize: '',
    nowColor: '',
    attributeValues: [],
    frameColor: [],
    oSrcImg: '',
    showIs: true, //判断显示隐藏
    lineIndex: 0 //选中的索引
  },
  methods: {
    suspendPackUp: function () {
      let $el = this.$refs.suspend.$el
      $el.style.height = '38px'
      setTimeout(() => {
        $el.removeAttribute('style')
      }, 20)
    },
    suspendPullDown: function () {
      let $el = this.$refs.suspend.$el
      $el.style.height = '154px'
    },
    goEdit() {
      if (this.showIs) {
        this.$message({
          showClose: true,
          iconClass: 'atrup_Message',
          message: '请先选择您需要定制的颜色 !',
          type: 'success'
        })
        return
      }
      if (localStorage.getItem("userDbId")) {
        location.href = "/album/maxImgEdit"
      } else {
        this.setUrlCallback()
      }
    },
    checkColor(index) {
      this.showIs = false
      this.nowColor = this.frameColor[index]
      this.lineIndex = index
      this.dynamicAlterUrl({
        color: this.lineIndex
      })
      this.getData(index)
      return true
    },
    /*更新sku*/
    updataSkuData() {
      this.skuCode = this.getQueryString('category', 'xiaoshiguang') + '.' + this.nowSize
      Api.sku.querySku({
        category: this.getQueryString('category', 'xiaoshiguang'),
        parameter: this.skuCode
      }).then(res => {
        bbsSlsectDate.tplCode = res.data.templateCode
        bbsSlsectDate.skuId = res.data.skuId
        bbsSlsectDate.price = res.data.price
        this.price = res.data.price
        this.oSrcImg = res.data.previewImageUrl
        bbsSlsectDate.category = res.data.category
        bbsSlsectDate.editCnfName = 'pc_baobaoshu_170-235_32_single'
        sessionStorage['titleName'] = '小时光'
        setTimeout(function () {
          sessionStorage['bbsSlsectDate'] = JSON.stringify(bbsSlsectDate)
          let tplCode = JSON.parse(sessionStorage['urlQuery'])
          tplCode.tplCode = res.data.templateCode
          sessionStorage['urlQuery'] = JSON.stringify(tplCode)
        }, 20)
      })
    },
    getData(index) {
      let setData = () => {
        let val = this.attributeValues
        bbsSlsectDate.name = [
          bbsSlsectDate._name,
          this.frameColor[index].name,
          val[2].attributeValues[0].code,
          val[1].attributeValues[0].code].join('.') + '页'
        bbsSlsectDate.skuCode = [
          bbsSlsectDate._code,
          this.frameColor[index].code,
          val[2].attributeValues[0].code,
          val[1].attributeValues[0].code].join('.')
        bbsSlsectDate.colorName = val[0].attributeValues[index].name
        this.nowSize = bbsSlsectDate.skuCode
        this.updataSkuData()
      }
      if (this.attributeValues.length) {
        setData()
        return
      }
      Api.sku.queryAttributes({
        category: this.getQueryString('category', 'xiaoshiguang')
      }).then(res => {
        this.attributeValues = res.data.attributes
        this.frameColor = this.attributeValues[0].attributeValues
        bbsSlsectDate._name = res.data.name
        bbsSlsectDate._code = res.data.code
        bbsSlsectDate.size = res.data.attributes[2].attributeValues[0].code
        this.nowColor = this.frameColor[0]
        setData()
      })
    }
  },
  mounted() {
    sessionStorage.setItem("urlQuery", JSON.stringify({
      'category': this.getQueryString('category', 'xiaoshiguang'),
      'defDbId': this.getQueryString('defDbId', '7ad740df-0b81-418f-b4b5-c078ef580b47'),
      'tplCode': this.getQueryString('tplCode', 'pc_baobaoshu_170-235_24')
    }))
    let colorIdx = +this.getQueryString('color', -1)
    if (colorIdx >= 0) {
      this.showIs = false
      this.lineIndex = colorIdx
    }
    this.getData(this.lineIndex)
    let that = this
    this.$nextTick(function () {
      that.showSuspensionHeight = that.$refs.showSuspensionFlag.offsetTop
    })
  },
  created: function () {
    document.body.style.cssText = 'opacity:1;'
  },
  components: {
    UnifyHeader,
    UnifyFooter,
    Suspension
  }
})

